package handler

import (
	"database/sql"

	"gitee.com/hp-huiw/family-expense-api/internal/service"
	"github.com/gin-gonic/gin"
)

type RoleHandler interface {
	QueryNonSystemLevelRoles(ctx *gin.Context)
	QueryAllRoles(ctx *gin.Context)
}

func NewRoleHandler(db *sql.DB) RoleHandler {
	return &roleHandler{
		roleService: service.NewRoleService(db),
	}
}

// Matches /query-non-system-level-roles of http get action.
func (h *roleHandler) QueryNonSystemLevelRoles(ctx *gin.Context) {
	c := ctx.Request.Context()
	result, err := h.roleService.QueryNonSystemLevelRoles(c)

	if err != nil {
		ctx.Error(err)
		return
	}

	SendDataResponse(ctx, result)
}

// Matches /query-all-roles of http get action.
func (h *roleHandler) QueryAllRoles(ctx *gin.Context) {
	c := ctx.Request.Context()
	result, err := h.roleService.Query(c)

	if err != nil {
		ctx.Error(err)
		return
	}

	SendDataResponse(ctx, result)
}

// Container of RoleHandler functions.
type roleHandler struct {
	roleService service.RoleService
}
